package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.PermissionsAliasConstants;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/level")
public class LevelController {

    @Autowired
    LevelService levelService;

    // 添加等级
    @SaAdminCheckPermission(PermissionsAliasConstants.Level.ADD)
    @PostMapping
    public Result<?> addLevel(@RequestBody LevelCommand command) {
        levelService.addLevel(command);
        return Result.ok();
    }

    // 编辑等级
    @SaAdminCheckPermission(PermissionsAliasConstants.Level.UPDATE)
    @PutMapping
    public Result<?> updateLevel(@RequestBody LevelCommand command) {
        levelService.updateLevel(command);
        return Result.ok();
    }


     // 分页获取等级列表
     // GET /admin/level/list?pageNum=2&count=5&keyword=LVL
     @SaAdminCheckPermission(PermissionsAliasConstants.Level.SEARCH)
    @GetMapping("/list")
    public Result<PageDTO<LevelDTO>> getLevelList(PageRequest request) {
        PageDTO<LevelDTO> page = levelService.getLevelList(request);
        return Result.ok(page);
    }


     // 删除单个等级
     // DELETE /admin/level/{id}
     @SaAdminCheckPermission(PermissionsAliasConstants.Level.DELETE)
    @DeleteMapping("/{id}")
    public Result<DeleteLevelOperateDTO> deleteLevel(@PathVariable Long id) {
        DeleteLevelOperateDTO dto = levelService.deleteLevel(id);
        return Result.ok(dto);
    }


     // 批量删除等级
     // DELETE /admin/level/batch
     // 请求体: { "ids": ["1", "2", "3"] }
     @SaAdminCheckPermission(PermissionsAliasConstants.Level.BATCH_DELETE)
    @DeleteMapping("/batch")
    public Result<List<DeleteLevelOperateDTO>> deleteLevels(@RequestBody BatchDeleteCommand command) {
        List<DeleteLevelOperateDTO> list = levelService.deleteLevels(command);
        return Result.ok(list);
    }

}
